package com.ntt.register.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ntt.register.biz.UserInfoBiz;
import com.ntt.register.biz.impl.UserInfoBizImpl;
import com.ntt.vo.UserInfo;

/**
 * Servlet implementation class AboutController
 */
public class UserInfoController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserInfoBiz userInfoBiz = new UserInfoBizImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserInfoController() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 相关设定
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		// 提取动作参数
		String action = request.getParameter("action");
		// 添加
		if ("registerAdd".equals(action)) {
			// 用户账户
			int usernum = Integer.parseInt(request.getParameter("usernum"));
			// 真实姓名
			String username = request.getParameter("username");
			// 性别
			String sex = request.getParameter("sex");
			// 民族
			String national = request.getParameter("national");

			// Date birthday = request.getParameter("birthday");

			// E-mail
			String email = request.getParameter("email");
			// 联系手机
			int phone = Integer.parseInt(request.getParameter("phone"));
			// 所在单位
			String work = request.getParameter("work");
			// 出生地
			String address = request.getParameter("address");
			// 密码
			String password = request.getParameter("password");
			// 确认密码
			String confirmPassword = request.getParameter("confirmPassword");
			UserInfo userInfo = new UserInfo();
			userInfo.setUsernum(usernum);
			userInfo.setUsername(username);
			userInfo.setSex(sex);
			userInfo.setNational(national);
			// userInfo.setBirthday(birthday);
			userInfo.setEmail(email);
			userInfo.setPhone(phone);
			userInfo.setWork(work);
			userInfo.setAddress(address);
			userInfo.setPassword(password);
			userInfo.setConfirmpwd(confirmPassword);
			boolean flag1 = true;
			if (flag1 == true) {
				File f = new File("C:\\data\\dk.txt");
				try {
					FileWriter txt = new FileWriter(f);
					txt.write(request.getParameter("usernum")+"\r\n");
					txt.write(request.getParameter("username")+"\r\n");
					txt.write(request.getParameter("sex")+"\r\n");
					txt.write(request.getParameter("national")+"\r\n");
					txt.write(request.getParameter("phone")+"\r\n");
					txt.write(request.getParameter("email")+"\r\n");
					txt.write(request.getParameter("year")+"\r\n");
					txt.write(request.getParameter("address")+"\r\n");
					txt.write(request.getParameter("work")+"\r\n");
					txt.write(request.getParameter("phone")+"\r\n");
					txt.close();
				} catch (IOException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}
			} 
			request.getRequestDispatcher("register.jsp").forward(request,
					response);
		}	
	}

}
